﻿using HslCommunication;
using HslCommunication.Profinet.Panasonic;
using System;
using System.Threading.Tasks;
using Xcj.Mes.Core.Logger;
using Xcj.Mes.WholeLine.Plc.Base;

namespace Xcj.Mes.WholeLine.Plc.Panasonic
{
    public class PanasonicPlc : PlcBase
    {
        private PanasonicMewtocolOverTcp plc;

        public PanasonicPlc(string deviceType) : base(deviceType) { }

        public override bool Connect()
        {
            if ((string.IsNullOrEmpty(IP)) || (Port <= 0)) return false;

            if (Mutex?.WaitOne(0) ?? false)
            {
                lock (lockAccess)
                {
                    try
                    {
                        Disconnect();
                        plc = new PanasonicMewtocolOverTcp(IP, Port);
                        if(plc == null) return false;
                        OperateResult result = plc.ConnectServer();
                        IsConnected = result.IsSuccess;
                        return IsConnected;
                    }
                    catch {}
                }
            }
            return false;
        }

        public override void Disconnect()
        {
            try { plc?.ConnectClose(); } catch { }
            IsConnected = false;
        }

        public override async Task DisconnectAsync()
        {
            try { await plc?.ConnectCloseAsync(); } catch { }
            IsConnected = false;
        }

        public override bool Read(string nStartAddr, ushort nCount, out bool[] values)
        {
            values = default(bool[]);
            try
            {
                OperateResult<bool[]> result = plc.ReadBool(nStartAddr, 1);
                if (!result.IsSuccess) return false;
                if ((result.Content?.Length ?? 0) <= 0) return false;
                values = new bool[result.Content.Length];
                Array.Copy(result.Content, values, result.Content.Length);
                return true;
            }
            catch {  }
            return false;
        }

        public override bool Read(string nStartAddr, ushort nCount, out byte[] values)
        {
            values = default(byte[]);
            try
            {
                OperateResult<byte[]> result = plc.Read(nStartAddr, nCount);
                if (!result.IsSuccess)
                {
                    //MyLogger.Log(result.Message);
                    return false;
                }
                   
                if ((result.Content?.Length ?? 0) <= 0) return false;
                values = new byte[result.Content.Length];
                Array.Copy(result.Content, values, result.Content.Length);
                return true;
            }
            catch { }
            return false;
        }

        public override bool Write(string nStartAddr, ushort count, bool[] datas)
        {
            try
            {
                OperateResult result = plc.Write(nStartAddr, datas);
                return result?.IsSuccess??false;
            }
            catch { }
            return false;
        }

        public override bool Write(string nStartAddr, ushort count, byte[] datas)
        {
            try
            {
                OperateResult result = plc.Write(nStartAddr, datas);
                return result?.IsSuccess ?? false;
            }
            catch { }
            return false;
        }
    }
}
